using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
//TODO: Create level manager to initialize grid, game objects, camera, behavior, etc
namespace Week1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game, helper.IDebugable
    {
        #region variables
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        player.Camera oCamera;
        helper.DebugConsole oDebug;
        Week1.world.terrain.GameGrid oGrid;
        #endregion

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            this.graphics.PreferredBackBufferHeight = 600;
            this.graphics.PreferredBackBufferWidth = 1024;
            this.graphics.IsFullScreen = false;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            oCamera = new player.Camera(this);
            world.terrain.GameGrid oGrid = world.terrain.GameGrid.getInstance(this.GraphicsDevice, oCamera);
            player.MousePointer oPointer = new player.MousePointer(this, "pointer", oCamera);
            world.SpaceDome oSpaceDome = new world.SpaceDome(this, "spacedome", oCamera, null);

            // init trigger manager
            world.behavior.TriggerManager oTriggerManager = world.behavior.TriggerManager.getInstance(this);

            world.DrawableBase oAstroid = new world.DrawableBase(this, "homeplanet", oCamera, null, 10);

            world.behavior.TriggerKey oTriggerKey = new world.behavior.TriggerKey();

            #region path behavior
            oTriggerKey.oTriggetType = world.behavior.TriggerKey.TriggetType.GameUpdate;
            world.behavior.BehaviorPath oPath = world.behavior.BehaviorPath.Instance;

            int iPath = oAstroid.registerBehavior(-1, oPath, oTriggerKey);

            long[] time = { 0, 2, 4, 6, 8, 9, 10, 11, 16, 18, 20 };

            oPath.setBehaviorProperty(iPath, -1, null, time);
            float x = -100;
            float y = -95;
            float z = 1;
            Vector3[] path = { new Vector3(x, y, z), new Vector3(x+=10, y, z+=5), 
                                 new Vector3(x+=10, y, z), new Vector3(x+=10, y, z-=5), 
                                 new Vector3(x+=10, y, z), new Vector3(x+=10, y , z),
                             new Vector3(x+=10,y,z),
                             new Vector3(x+=5,y,z),
                             new Vector3(x,y+=40,z),
                             new Vector3(x-=30,y,z),
                             new Vector3(x,y+=10,z),
                             new Vector3(x+=5,y,z),
                             new Vector3(x+=10,y,z+=5),
                             new Vector3(x+=15,y,z),
                             new Vector3(x-=10,y,z),
                             new Vector3(x,y+=10,z),
                             new Vector3(x-=5,y,z),
                             new Vector3(x-=10,y,z-=5),
                             new Vector3(x-=35,y,z),
                             };
            oPath.setBehaviorProperty(iPath, -1, null, path);
            oPath.setBehaviorProperty(iPath, -1, null, .1f);
            //Vector2[] path = { new Vector2(5, 0), new Vector2(15, 0), new Vector2(15, 15) };
            //oPath.setBehaviorProperty(iPath, -1, null, path);
            #endregion

            #region move Straight behavior
            /*
            oTriggerKey.oTriggetType = world.behavior.TriggerKey.TriggetType.GameTimeSec;
            oTriggerKey.lSecStartTime = 14;
            oTriggerKey.lSecEndTime = 60;
            int iBehaviorHdl = oAstroid.registerBehavior(-1, world.behavior.BehaviorMoveStraight.Instance, oTriggerKey);
            
            Random oRnd = new Random((int) DateTime.Now.Millisecond);
            string[] direction = { "east", "west", "north", "south" };

            for (int iDx = 0; iDx < oAstroid.oVars.iInstanceCount; iDx++)
            {
                float fspeed = (float) oRnd.Next(1, 5) / 10;

                oAstroid.oVars.oBehavior[0].setBehaviorProperty(iBehaviorHdl, iDx, direction[oRnd.Next(0,4)], fspeed);
            }
            */
            #endregion

            #region Revolve behavior
            /*
            oTriggerKey = new world.behavior.TriggerKey();
            oTriggerKey.oTriggetType = world.behavior.TriggerKey.TriggetType.GameTimeSec;
            oTriggerKey.lSecStartTime = -1;
            oTriggerKey.lSecEndTime = 15;
            iBehaviorHdl = oAstroid.registerBehavior(-1, world.behavior.BehaviorRevolve.Instance, oTriggerKey);
            */
            #endregion

            this.Components.Add(oTriggerManager);
            this.Components.Add(oCamera);
            this.Components.Add(oSpaceDome);
            this.Components.Add(oAstroid);

            this.Components.Add(oPointer);

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            oDebug = helper.DebugConsole.Instance;
            oDebug.Initialize(this, oCamera);
            oDebug.registerDebug(this);

            oGrid = world.terrain.GameGrid.getInstance(this.GraphicsDevice, oCamera);

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            //long starttime = DateTime.Now.Ticks * 100;
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();


            oDebug.update(gameTime);

            base.Update(gameTime);
            //long delay = DateTime.Now.Ticks * 100 - starttime;
            //delay *= 1;

        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            //long starttime = DateTime.Now.Ticks * 100;

            this.GraphicsDevice.Clear(Color.Black);

            oDebug.PreDraw(gameTime);
            base.Draw(gameTime);
            oGrid.drawDebug();
            oDebug.draw(gameTime);

            //long delay = DateTime.Now.Ticks * 100 - starttime;
            //delay *= 1;

        }

        #region debug
        helper.DebugMsg oMsg = new helper.DebugMsg();
        Vector3 oDebugPosition = new Vector3(0, 0, 0);
        helper.DebugMsg helper.IDebugable.getDebugMsg(bool showNextInstance)
        {
            oMsg.message = this + "\n components = " + this.Components.Count;
            oMsg.position = oDebugPosition;
            oMsg.scale = 40;
            return oMsg;
        }
        #endregion
    }
}